package com.xiaolong.websocket;

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class WsController {
    
	@Autowired
    private SimpMessagingTemplate messagingTemplate;//1
	
	@MessageMapping("/welcome")//1
    @SendTo("/topic/getResponse")//2
    public ResultData say(MessageData message) throws Exception {
        return new ResultData("Welcome, " + message.getMessage() + "!");
    }
	
	@MessageMapping("/chat")
    public void handleChat(Principal principal, String msg) { //2
        if (principal.getName().equals("dzz")) {//3
            messagingTemplate.convertAndSendToUser("zbb",
                    "/queue/notifications", principal.getName() + "-send:"
                            + msg);
        } else {
            messagingTemplate.convertAndSendToUser("dzz",
                    "/queue/notifications", principal.getName() + "-send:"
                            + msg);
        }
    }
}